package com.cjcx.aiserver;


import com.cjcx.aiserver.ai.utils.HttptUtils;
import com.cjcx.aiserver.ai.utils.ImageUtils;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 人脸检测与属性分析
 */
public class FaceDetect {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String detect() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
        try {
            File file = new File("C:\\Users\\guo\\Desktop\\psb.jpg");

            InputStream in = new FileInputStream(file);
            String image = ImageUtils.getBase64ByInputStream(in);


            //System.out.println(image);

            //image ="";

            Map<String, Object> map = new HashMap<>();
            map.put("image", image);
            map.put("image_type", "BASE64");
            map.put("face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,race,quality,face_type");
            //map.put("access_token", "24.2d413be1ab07c0ef18b01a7d724fdfa5.2592000.1536207656.282335-11642068");

            url = url + "?access_token=24.38a9a26d8b3e810089d9ff6e8d3cacfe.2592000.1539976300.282335-11642068";

            Gson gson = new Gson();
            String param = gson.toJson(map);

            String result = HttptUtils.doPostByJson(url, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        FaceDetect.detect();
    }
}
